#!/usr/bin/env python3
"""
CloudPilot 后端主程序
FastAPI 服务入口点
"""

import uvicorn
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles

from app.routers import auth, resources, monitor

# 创建 FastAPI 应用
app = FastAPI(
    title="CloudPilot API",
    description="轻量级云资源管理与监控平台 API",
    version="1.0.0"
)

# 配置 CORS
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# 注册路由
app.include_router(auth.router, prefix="/api/auth", tags=["认证"])
app.include_router(resources.router, prefix="/api", tags=["资源管理"])
app.include_router(monitor.router, prefix="/api", tags=["监控"])

# 挂载静态文件
app.mount("/", StaticFiles(directory="../frontend", html=True), name="static")

@app.get("/")
async def root():
    """根路径重定向到前端页面"""
    return {"message": "CloudPilot API 服务运行中"}

@app.get("/health")
async def health_check():
    """健康检查接口"""
    return {"status": "healthy", "service": "CloudPilot API"}

if __name__ == "__main__":
    uvicorn.run(
        "main:app",
        host="0.0.0.0",
        port=8000,
        reload=True
    )
